import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class TextFieldRoute extends StatefulWidget {
  const TextFieldRoute({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _TextFieldRouteState();
}

class _TextFieldRouteState extends State<TextFieldRoute> {
  final logger = Logger(printer: PrettyPrinter());

  final TextEditingController _username = TextEditingController();
  final TextEditingController _password = TextEditingController();

  final FocusNode _node1 = FocusNode();
  final FocusNode _node2 = FocusNode();
  FocusScopeNode? _scopeNode;

  @override
  void initState() {
    super.initState();
    _username.text = "hello world!";
    _username.selection =
        TextSelection(baseOffset: 2, extentOffset: _username.text.length);
    _username.addListener(() {
      logger.i(_username.text);
    });
    // 监听焦点变化
    _node1.addListener(() {
      logger.i(_node1.hasFocus);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('输入框'),
      ),
      body: SingleChildScrollView(
        child: Theme(
          data: Theme.of(context).copyWith(
            highlightColor: Colors.grey[200], //定义下划线颜色
            inputDecorationTheme: const InputDecorationTheme(
                labelStyle: TextStyle(color: Colors.grey), //定义label字体样式
                hintStyle:
                    TextStyle(color: Colors.grey, fontSize: 14.0) //定义提示文本样式
                ),
          ),
          child: Center(
            child: Column(
              children: <Widget>[
                TextField(
                  decoration: const InputDecoration(
                    labelText: "用户名",
                    hintText: "用户名或邮箱",
                    prefixIcon: Icon(Icons.person),
                  ),
                  autofocus: true,
                  focusNode: _node1, //关
                  controller: _username,
                ),
                SizedBox.fromSize(size: const Size.fromHeight(20)),
                TextField(
                  decoration: const InputDecoration(
                    labelText: "密码",
                    hintText: "登录密码",
                    prefixIcon: Icon(Icons.lock),
                    hintStyle: TextStyle(color: Colors.grey, fontSize: 10.0),
                  ),
                  focusNode: _node2, //关联focusNode2
                  controller: _password,
                  obscureText: true,
                ),
                SizedBox.fromSize(size: const Size.fromHeight(20)),
                Container(
                  child: const TextField(
                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                      hintText: "邮箱",
                      prefixIcon: Icon(Icons.email),
                      border: InputBorder.none,
                    ),
                  ),
                  margin: const EdgeInsets.symmetric(horizontal: 20),
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.blue, width: 1),
                    borderRadius: BorderRadius.circular(5),
                  ),
                ),
                SizedBox.fromSize(size: const Size.fromHeight(20)),
                Builder(builder: (context) {
                  return Column(
                    children: <Widget>[
                      ElevatedButton(
                        child: const Text("移动焦点"),
                        onPressed: () {
                          //将焦点从第一个TextField移到第二个TextField
                          // 这是一种写法 FocusScope.of(context).requestFocus(focusNode2);
                          // 这是第二种写法
                          _scopeNode ??= FocusScope.of(context);
                          _scopeNode?.requestFocus(_node2);
                        },
                      ),
                      SizedBox.fromSize(size: const Size.fromHeight(20)),
                      ElevatedButton(
                        child: const Text("隐藏键盘"),
                        onPressed: () {
                          _node1.unfocus();
                          _node2.unfocus();
                        },
                      )
                    ],
                  );
                }),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
